using System;

namespace NumSkull
{
    public class Bounds : IEquatable<Bounds>
    {
        public Bounds(double lower, double upper)
        {
            if (lower > upper) throw new ArgumentOutOfRangeException("lower");

            Lower = lower;
            Upper = upper;
        }

        public double Lower { get; private set; }
        public double Upper { get; private set; }
        public double Range { get { return Upper - Lower; } }
        public static Bounds Empty = new Bounds(0, 0);

        public bool IsInRange(double value)
        {
            var result = value >= Lower && value <= Upper;
            return result;
        }

        public bool Equals(Bounds other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;

            var isEqual = Equals(Lower, other.Lower) && Equals(Upper, other.Upper);
            return isEqual;
        }

        public override bool Equals(object obj)
        {
            var bounds = obj as Bounds;
            return Equals(bounds);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Lower.GetHashCode()*397) ^ Upper.GetHashCode();
            }
        }
    }
}